package connection.layer;


import model.layer.*;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.io.Serializable;
import java.util.*;


@SuppressWarnings("serial")
public class WCFConnection implements Serializable
{

	private SoapObject req;
	/*
	private static final String SOAP_ACTION = "http://94.191.189.90:3173/IWcfFoodAndAllergi.cs";
	private static String METHOD_NAME;// = "GetUser";
	private static final String NAMESPACE = "http://94.191.189.90:3173/";
	private static final String URL = "http://94.191.189.90:3173/WcfFoodAndAllergi.svc?singleWsdl";
	*/
	private String BASE_SOAP_ACTION = "http://tempuri.org/";
	private static String SOAP_ACTION;
	private static String METHOD_NAME;// = "GetUser";
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String URL = "http://192.168.1.18/WcfFoodAndAllergi.svc?singleWsdl";
	private HttpTransportSE transport;
	private SoapSerializationEnvelope soapEnvel;
	
	//Get and Setters
	public SoapSerializationEnvelope getSoapEnvel() {
		return soapEnvel;
	}
	public void setSoapEnvel(SoapSerializationEnvelope soapEnvel) {
		this.soapEnvel = soapEnvel;
	}
	public HttpTransportSE getTransport() {
		return transport;
	}
	public void setTransport(HttpTransportSE transport) {
		this.transport = transport;
	}
	public static String getMethodName() 
	{
		return METHOD_NAME;
	}
	
	
	public WCFConnection() 
	{
		req = new SoapObject();
	}
	
	private void Envelope()
	{
		soapEnvel = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvel.dotNet = true;

		soapEnvel.setOutputSoapObject(req);
		
		setTransport(new HttpTransportSE(URL));
		setSoapEnvel(soapEnvel);
		
	}
	
	//User
	public User GetUser(String _input)
	{
		METHOD_NAME = "GetUser";
		User result = new User();
		try
		{
			req = new SoapObject(NAMESPACE, METHOD_NAME);
			
			req.addProperty("_mac", _input);
			Envelope();
			transport.call(SOAP_ACTION, soapEnvel);
			result = (User)soapEnvel.getResponse();
			return result;
		}//end try
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}//end catch
	}//End getUser
	public int InsertUser(User _user)
	{
		METHOD_NAME = "InsertUser"; 
		SOAP_ACTION = BASE_SOAP_ACTION + METHOD_NAME;
		Object result;
		int retInt = -1;
		try
		{
			req = new SoapObject(NAMESPACE, METHOD_NAME);
			
			req.addProperty("User", _user);
			
			Envelope();
			soapEnvel.addMapping(NAMESPACE, "User", _user.getClass());
			transport.call(SOAP_ACTION, soapEnvel);
			result = soapEnvel.getResponse();
			retInt = (Integer) result;
			return retInt;
		}//end try
		catch (Exception e)
		{
			e.printStackTrace();
			return retInt;
		}
	}
	

	
	//Recipe
	public Recipe GetRecipeByType(String _type)
	{
		METHOD_NAME = "GetRecipeByType";
		Recipe result = null;
		try
		{
			req = new SoapObject(NAMESPACE, METHOD_NAME);
			
			req.addProperty("_typeOf", _type);
			Envelope();
			
			transport.call(SOAP_ACTION, soapEnvel);
			result = (Recipe) soapEnvel.getResponse();
			return result;
		}//end try
		catch (Exception e)
		{
			return result;
		}
	}
	Thread thread = new Thread(new Runnable() {
		
		@Override
		public void run() {
			GetUser("13-DA-8D-63-7E-63");
			
		}
	});
		
	@SuppressWarnings("unused")
	public ArrayList<Recipe> GetRecipeByName(String _name)
	{
		METHOD_NAME = "GetRecipeByName";
		SOAP_ACTION += METHOD_NAME;
		ArrayList<Recipe> ret = new ArrayList<Recipe>();
		try
		{
			req = new SoapObject(NAMESPACE, METHOD_NAME);
			
			req.addProperty("_name", _name);
			
			Envelope();
			
			transport.call(SOAP_ACTION, soapEnvel);
			
			SoapObject obj1 = (SoapObject)soapEnvel.getResponse();
			SoapObject obj2 = (SoapObject)soapEnvel.getResponse();
			
			for(int i=0; i<obj2.getPropertyCount(); i++)
			{
				Recipe temRecipe = new Recipe();
				temRecipe.setId(Integer.parseInt(obj2.getProperty(0).toString()));
				temRecipe.setName((String)obj2.getPropertyAsString(1));
				temRecipe.setTypeOf((String)obj2.getPropertyAsString(2));
				temRecipe.setServings(Integer.parseInt(obj2.getProperty(3).toString()));
				temRecipe.setPhotoId(Integer.parseInt(obj2.getProperty(4).toString()));
				temRecipe.setSummary((String)obj2.getPropertyAsString(5));
				
				ret.add(temRecipe);
			}//end for
			
			return ret;
		}//end try
		catch (Exception e)
		{
			return ret;
		}
		
	}
	public int InsertRecipe(Recipe _recipe)
	{
		METHOD_NAME = "InsertRecipe";
		Object result;
		int retInt = -1;
		
		try
		{
			req = new SoapObject(NAMESPACE, METHOD_NAME);
			
			req.addProperty("Recipe", _recipe);
			
			Envelope();
			transport.call(SOAP_ACTION, soapEnvel);
			result = soapEnvel.getResponse();
			retInt = (Integer) result;
			return retInt;
		}//end try
		catch (Exception e)
		{
			return retInt;
		}
	}
	/*public ArrayList<Recipe> GetRecipeByAllergies(ArrayList<Allergies> _allergies)
	{
		METHOD_NAME = "GetRecipeByAllergi";
		//Object result;
		ArrayList<Recipe> ret = new ArrayList<Recipe>();
		
		try
		{
			req = new SoapObject(NAMESPACE, METHOD_NAME);
			
			req.addProperty("List<Allergies>", _allergies);
			
			Envelope();
			transport.call(SOAP_ACTION, soapEnvel);
			//result = soapEnvel.getResponse();
			
			SoapObject obj1 = (SoapObject) soapEnvel.getResponse();
			
			SoapObject obj2 = (SoapObject) obj1.getProperty(0);
			
			for(int i=0; i<obj2.getPropertyCount(); i++)
			{
				Recipe temRecipe = new Recipe();
				temRecipe.setId(Integer.parseInt(obj2.getProperty(0).toString()));
				temRecipe.setName((String)obj2.getPropertyAsString(1));
				temRecipe.setTypeOf((String)obj2.getPropertyAsString(2));
				temRecipe.setServings(Integer.parseInt(obj2.getProperty(3).toString()));
				temRecipe.setPhotoId(Integer.parseInt(obj2.getProperty(4).toString()));
				temRecipe.setSummary((String)obj2.getPropertyAsString(5));
				
				ret.add(temRecipe);
			}//end for
			
			return ret;
		}//end try
		catch (Exception e)
		{
			return ret;
		}
		
	}*/
}	
	
